﻿using SMS_Agent.Model;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace SMS_Agent.BLL
{
    public abstract class BaseBLL_Provider
    {
        protected SMS_Agent.Model.Entities dbEntities;

        public string ErrMsg { get; set; }

        public user Actor { get; set; }

        protected BaseBLL_Provider()
        {
            this.dbEntities = getDbEntities();
        }

        protected BaseBLL_Provider(SMS_Agent.Model.Entities entities)
        {
            this.dbEntities = entities;
        }

        protected void ThrowExceptionWithErrMsg()
        {
            throw new System.Exception(ErrMsg);
        }

        protected bool GetActor()
        {
            Actor = RootStaticVar.CurrentUser;
            return true;
        }

        protected bool CheckActorExist()
        {
            bool res = true;
            User_BLL bll = new User_BLL(this.dbEntities);
            user user = bll.FilterById(this.Actor.user_name);
            if (user == null)
            {
                ErrMsg = "Nhân viên không tồn tại";
                res = false;
            }
            return res;
        }

        public void DiscardAllChanges()
        {
            DiscardAllChanges(dbEntities);
        }

        public void SaveChanges()
        {
            dbEntities.SaveChanges();
        }
        
        protected static void DiscardAllChanges(DbContext context)
        {
            // Undo the changes of the all entries. 
            foreach (DbEntityEntry entry in context.ChangeTracker.Entries())
            {
                switch (entry.State)
                {
                    // Under the covers, changing the state of an entity from  
                    // Modified to Unchanged first sets the values of all  
                    // properties to the original values that were read from  
                    // the database when it was queried, and then marks the  
                    // entity as Unchanged. This will also reject changes to  
                    // FK relationships since the original value of the FK  
                    // will be restored. 
                    case EntityState.Modified:
                        entry.State = EntityState.Unchanged;
                        break;
                    case EntityState.Added:
                        entry.State = EntityState.Detached;
                        break;
                    // If the EntityState is the Deleted, reload the date from the database.   
                    case EntityState.Deleted:
                        entry.Reload();
                        break;
                    default: break;
                }
            }
        }

        public static Model.Entities getDbEntities()
        {
            if (RootStaticVar.DbEntities == null)
            {
                //RootStaticVar.DbEntities = new Model.Entities(RootStaticVar.EntityConnection);
                //RootStaticVar.DbEntities = new Model.AnimalCareEntities(1);
                RootStaticVar.DbEntities = new Model.Entities();
            }
            return (Model.Entities)RootStaticVar.DbEntities;

            //return (new AnimalCareEntities());
        }

    }
}